import Vue from 'vue'
import Router from 'vue-router'
import { getStore } from '../../src/config/mUtils'
// import '../router/router'

const lists = r => require.ensure([], () => r(require('@/pages/issue/lists'), 'lists'));
const detail = r => require.ensure([], () => r(require('@/pages/issue/detail'), 'detail'));
const collect = r => require.ensure([], () => r(require('@/pages/user/collect'), 'collect'));
const publish = r => require.ensure([], () => r(require('@/pages/user/publish'), 'publish'));
const autonym = r => require.ensure([], () => r(require('@/pages/user/autonym'), 'autonym'));
const Home = r => require.ensure([], () => r(require('@/pages/home/home'), 'home'));
const issue = r => require.ensure([], () => r(require('@/pages/issue/issue'), 'issue'));
const message = r => require.ensure([], () => r(require('@/pages/message/message'), 'message'));
const caseDetail = r => require.ensure([], () => r(require('@/pages/message/caseDetail'), 'caseDetail'));
const user = r => require.ensure([], () => r(require('@/pages/user/user'), 'user'));
const AccountData = r => require.ensure([], () => r(require('@/pages/user/accountdata'), 'accountdata'));
const setPsd = r => require.ensure([], () => r(require('@/pages/user/setPsd'), 'setPsd'));
const signAward = r => require.ensure([], () => r(require('@/pages/user/signintegral'), 'signintegral'));
const CreditNum = r => require.ensure([], () => r(require('@/pages/user/CreditNum'), 'CreditNum'));
const coin = r => require.ensure([], () => r(require('@/pages/user/coin'), 'coin'));
const login = r => require.ensure([], () => r(require('@/pages/user/login'), 'login'));
const register = r => require.ensure([], () => r(require('@/pages/user/register'), 'register'));
const contact = r => require.ensure([], () => r(require('@/pages/user/contact'), 'contact'));
const isdetail = r => require.ensure([], () => r(require('@/pages/issue/isdetail'), 'isdetail'));
const searchDetail = r => require.ensure([], () => r(require('@/pages/message/searchDetail'), 'searchDetail'));
const cityDetail = r => require.ensure([], () => r(require('@/pages/home/cityDetail'), 'cityDetail'));
const forgetPsd = r => require.ensure([], () => r(require('@/pages/user/forgetPsd'), 'forgetPsd'));
const sku = r => require.ensure([], () => r(require('@/pages/sku/sku'), 'sku'));
const Agreement = r => require.ensure([], () => r(require('@/pages/user/userAgreement'), 'userAgreement'));
const TopicList = r => require.ensure([], () => r(require('@/pages/issue/TopicList'), 'TopicList'));
const serviceAgreement = r => require.ensure([], () => r(require('@/pages/user/serviceAgreement'), 'serviceAgreement'));


Vue.use(Router)

export default new Router({
  routes: [{
    path: '*',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'home',
    component: Home
  },
  {
    path: '/issue',
    name: 'issue',
    component: issue,
    children: [{
      path: '/issue',
      component: isdetail
    },]
  },
  {
    path: '/message',
    name: 'message',
    component: message
  },
  {
    path: '/user',
    name: 'user',
    meta: { requireAuth: true },
    component: user
  },
  {
    path: '/AccountData',
    name: 'AccountData',
    component: AccountData
  },
  {
    path: '/lists',
    name: 'lists',
    component: lists,
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/detail/:id',
    name: 'detail',
    component: detail
  },
  {
    path: '/setPsd',
    name: 'setPsd',
    component: setPsd
  },
  {
    path: '/collect',
    name: 'collect',
    component: collect
  },
  {
    path: '/caseDetail',
    name: 'caseDetail',
    component: caseDetail
  },
  {
    path: '/publish',
    name: 'publish',
    component: publish
  },
  {
    path: '/autonym',
    name: 'autonym',
    component: autonym,
  },
  {
    path: '/signAward',
    name: 'signAward',
    component: signAward
  },
  {
    path: '/CreditNum',
    name: 'CreditNum',
    component: CreditNum
  },
  {
    path: '/coin',
    name: 'coin',
    component: coin
  },
  {
    path: '/login',
    name: 'login',
    component: login
  },
  {
    path: '/register',
    name: 'register',
    component: register
  },
  {
    path: '/contact',
    name: 'contact',
    component: contact
  },
  {
    path: '/searchDetail',
    name: 'searchDetail',
    component: searchDetail
  },
  {
    path: '/cityDetail',
    name: 'cityDetail',
    component: cityDetail
  },
  {
    path: '/forgetPsd',
    name: 'forgetPsd',
    component: forgetPsd
  },
  {
    path: '/sku',
    name: 'sku',
    component: sku
  },
  {
    path: '/Agreement',
    name: 'Agreement',
    component: Agreement
  },
  {
    path: '/TopicList',
    name: 'TopicList',
    component: TopicList
  },
  {
    path: '/serviceAgreement',
    name: 'serviceAgreement',
    component: serviceAgreement
  },
  ],
  scrollBehavior (to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  },

});
// router.beforeEach((to, from, next) => {
//   if (to.meta.requireAuth) {
//     //判断该路由是否需要登录权限
//     if (getStore('token')) {
//       //通过封装好的cookies读取token，如果存在，name接下一步如果不存在，那跳转回登录页
//       next()//不要在next里面加"path:/",会陷入死循环
//     }
//     else {
//       next({
//         path: '/login',
//         query: {redirect: to.fullPath}//将跳转的路由path作为参数，登录成功后跳转到该路由
//       })
//     }
//   }
//   else {
//     next()
//   }
// })
